package com.wish.wishhouse_foreclosure.service.impl;

import com.wish.wishhouse_foreclosure.entity.TMSequence;
import com.wish.wishhouse_foreclosure.mapper.TMSequenceMapper;
import com.wish.wishhouse_foreclosure.service.ITMSequenceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Synchronized;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

/**
 * <p>
 * 序号表（取编号用） 服务实现类
 * </p>
 *
 * @author weijiaz
 * @since 2021-04-05
 */


@Slf4j
@Service
public class TMSequenceServiceImpl extends ServiceImpl<TMSequenceMapper, TMSequence> implements ITMSequenceService {

    @Override
    @Synchronized
    public String getSerialNumber(String type) throws Exception {
        try {
//            log.info("------生成编号   start-------");
            // TransactionStatus beginTransaction = this.beginTransaction(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            String result = baseMapper.getSerialNumber(type);
           // this.commit(beginTransaction);
            if(!StringUtils.isNotBlank(result)) {
//                log.info("------未获取到编号-------");
                throw new Exception("未获取到编号");
            }
            int random = (int) ((Math.random()*9+1)*1000);
            result = type + result + random;
//            log.info("------生成编号End,返回生成的编号是{}", result);
            return result;
        } catch (Exception e) {
//            log.info("------生成编号失败-------");
            throw e;
        }
    }

}
